from utils.redis import RedisUtil
from fastapi import Request, HTTPException
from fastapi.security import SecurityScopes


async def global_filter(
    req: Request,
    sp: SecurityScopes=[]
):
    
    if req.url.path in ['/api/auth/login', '/api/auth/logout']:
        return True
    
    token = req.headers.get("token")
    if token==None:
        raise HTTPException(
            status_code=401,
            detail="无访问呢权限",
        )

    user_id = await RedisUtil.get_data(req.app, token)

    if user_id is None:
        raise HTTPException(
            status_code=401,
            detail="无访问呢权限",
        )
    
    else:
        
        req.app.state.user_id = user_id
        return True